NEW PROJECT TUTORIAL

Introduction

The purpose of this tutorial is to show you, from first principles, how to conduct an interactive simulation using Proteus VSM. Both the schematic and the firmware are kept as simple as possible but the techniques covered will scale to projects of any size.

VSM Studio is not a requirement for a VSM simulation. You can attach a compiled firmware file to the processor part by hand, or drive the simulation from a third party IDE, but for a short project such as this one the integrated route shown here is the quickest way to get running.

The circuit we will be using for the simulation is a pair of traffic lights connected to a PIC16F84A microcontroller as shown below:

Two sets of traffic lights wired to a PIC16F84A microcontroller on the schematic

The New Project Wizard

The easiest way to get started with a new project is to use the New Project Wizard on the home page. The home page appears when you first open Proteus or can be launched from the module toolbar at the top of the application.

The Proteus home page, with the New Project button on the Start bar

The first page of the wizard allows you to specify a name and destination directory for the project.

The Start page of the New Project Wizard, where the project name and path are entered

We will need a schematic design so check the box at the top of the next page and select a suitable size (default is fine).

The Schematic Design page of the New Project Wizard, with the list of design templates

We don't need a PCB layout for this project so if you see this page you can skip past it. If you do not have a licence for a PCB product this page will not appear in the wizard.

On the firmware page of the wizard we need to do some configuration. First select the Create Firmware Project option and then specify the family and the microcontroller.

For simplicity we are going to write our little program in assembly and will use the MPASM assembler installed with the Proteus software. All we need to do therefore is select the pre-configured MPASM assembler from the compiler drop-down list.

This will narrow down the list of supported compilers to those applicable to the specified controller. Depending on whether the compiler is installed on your computer or not, its entry in the combo box will have a 'not configured' suffix.

The firmware page of the New Project Wizard, with the compiler drop-down list open

If you want to use a compiler that is not installed then you can click on the compilers button at the right hand side. The resulting dialogue will either let you download directly from Labcenter's website or will take you to the download page on the compiler vendor site. In either case, your procedure would be to install the compiler and then click on the 'check all' button at the bottom of the dialogue form to enable its Proteus configuration.

The Compilers dialogue, listing each compiler with its installed state and directory, and the Check All button at the foot of the form

See Also :

Compilers and Compiler Detection.

Our last option is to create some skeleton firmware files. This is very useful as it will also set up linker scripts for us so leave this selected.

Configured settings for the tutorial project.

Configured settings for the tutorial project.

If we were working with Arduino then we could also check the option for 'Create Peripherals'. This enables the use of Labcenter peripheral drivers for Arduino shields and breakout boards, making development with these devices much simpler.

See Also:

Working with Arduino

The last page of the wizard is then a summary of our configuration before we complete and create the project.

 

The Summary page of the New Project Wizard, listing the schematic and firmware settings chosen for the project

Exploring the Project

When you exit the wizard Proteus will create a new project which at first sight will look something like the following.

The new project on first opening, showing the Schematic Capture and VSM Studio tabs, the project tree and the skeleton main.asm source file

Quite a lot has happened at this point. Proteus has created two modules, one for the schematic capture design and the second for your firmware development. These modules are docked as tabs and you can switch between them by clicking on the tab at the top of the application (or via the CTRL+TAB shortcut key).

You can also split screen the two tabs by dragging one onto another monitor or a free area of your desktop.

The VSM Studio module has created a firmware project for you, populated it with a skeleton source file and configured the compile options for use with Proteus VSM. Meanwhile, in the Schematic Capture module the selected processor has been placed and powered.

We now need to complete our schematic design and write the firmware code to drive the simulation.

Drawing the Circuit

Note that the circuit will be auto-drawn if you are working with Arduino as all you need to do is import the shields or breakout boards.

We will start by placing and wiring two sets of traffic lights on the schematic. Since much of the detail of schematic placement is covered in the schematic tutorial we will move through this section fairly quickly.

Click on the schematic capture tab to enter the schematic module and then select component mode and use the 'P' button above the Object Selector to launch the Library Browser (the form itself is titled Pick Devices).

Component mode selected on the toolbar and the P button above the Object Selector

Type 'traffic lights' in the keywords list and then double click on the item in the results list to pick the part into the design. You should then see it appear in the object selector.

The library browser searched on the keywords traffic lights, with the picked part listed in the Object Selector

Close the library browser and click the mouse once on the traffic lights part in the object selector. This will highlight your selection and a preview of the device will appear in the Overview Window.

A preview of the traffic lights part in the Overview Window, with the part highlighted in the Object Selector

Now, left click on the Editing Window to enter placement mode, move the mouse to the desired location and left click again to place the part on the schematic. Repeat the process to place the second set of traffic lights.

Movement and Orientation

Our wiring will be far simpler if the traffic lights are placed to the right of the processor. To move a component, point the mouse at it, right click (this will highlight the component) and select 'Drag Object' from the resulting context menu.

The Drag Object command on the context menu of a traffic lights part

You can then move the mouse which will 'carry' the component and left click to place in its desired location. At this stage you want something like the following.

The two sets of traffic lights positioned to the right of the microcontroller, ready for wiring

Zooming

In order to wire up the schematic it is useful to be able to zoom in to a specific area. The easiest way to do this is to hold down the SHIFT key and drag a box around the area you want to zoom into. Alternatively, you can use the middle mouse wheel or the F6 and F7 keyboard shortcuts to zoom in and out around the current mouse position. You can revert to a full screen view at any time using the F8 shortcut key. Finally, menu icons at the top of the application provide access to the same functions.

The pan and zoom buttons on the schematic toolbar

Wiring Up

Zoom in to the PIC microcontroller until all of the pins are visible and then place the mouse pointer over the end of pin 6 (RB0/INT). The mouse cursor will change to a green pen to indicate that you are on the pin tip and can start wire placement.

The green pen cursor shown at the tip of pin RB0/INT, ready to start a wire

Left click the mouse to start a wire and then move the mouse to the pin connected to the red light on one of the sets of traffic lights. When you arrive at the pin on the traffic lights the mouse cursor will again change colour and you can left click to commit the wire placement.

A completed wire running from a processor pin to the red light pin on a set of traffic lights

Repeat the process to wire up both sets of traffic lights, using the following as a guide.

The finished schematic with both sets of traffic lights wired to the microcontroller

If you need more control over wire placement you can left click to change direction at any time while placing a wire.

If you make a mistake during wire placement use the ESCAPE key to abort the wire. If you make a wiring error you can delete the wire by right clicking the mouse on the wire and selecting Delete Wire from the resulting context menu.

The Delete Wire command on the context menu of a schematic wire

There are a couple of points worth noting about the wiring process:

  • You can wire up in any mode: the schematic module is clever enough to determine what you are doing.
  • You can left click the mouse during wire placement to commit existing wiring and change direction at that point.
  • The schematic module will automatically pan the screen if you nudge the edge of the Editing window while placing a wire. This means that you can zoom in to the most suitable level for placing the wire and, so long as you know the approximate position of the target component, simply nudge the screen over until it is in view. Alternatively, you can zoom in and out while placing wires using either middle mouse wheel or the F6 and F7 keys.

We now have a simple schematic which we can drive by writing some firmware.

Writing the Program

Click on the VSM Studio tab to switch the application to the firmware IDE. For the purposes of this tutorial we will use the following program which will enable the PIC to control the traffic lights. You can either type directly or copy and paste the following into the source file in VSM Studio.

 

;====================================================================

; DEFINITIONS

;====================================================================

#include p16f84a.inc ; Include register definition file

;====================================================================

; VARIABLES

;====================================================================

cblock 0x10 ; Temporary storage

state

l1,l2

endc

;====================================================================

; RESET and INTERRUPT VECTORS

;====================================================================

; Reset Vector

RST code 0x0

goto setports

;====================================================================

; CODE SEGMENT

;====================================================================

PGM code

setports

clrw

movwf PORTA

movwf PORTB

bsf STATUS,RP0

clrw

movwf TRISB

bcf STATUS,RP0

initialise

clrw

movwf PORTB

loop

call getmask

movwf PORTB

incf state,W

andlw 0x04

movwf state

call wait

goto loop

; Function to return bitmask for output port for current state.

; Top nibble controls one set of lights and bottom nibble the other.

getmask

movf state, W

addwf PCL,F

retlw 0x41 ; Green and Red.

retlw 0x23 ; Amber and Red/Green.

retlw 0x14 ; Red and Green.

retlw 0x32 ; Red/Amber and Green.

; Simple delay routine.

wait

movlw 0xFF

movwf l1

w1 decfsz l1

goto w1

return

wait2 clrf l2

w2 decfsz l2

goto w2

return

;======================================================

END

See Also:

Firmware Development

There is, in fact, a deliberate mistake in this program but we'll come back to that later.

Compilation

If need be, project settings can be accessed from the context menu after right clicking on the project name, from the Project menu, or from the Project Settings button on the Build toolbar.

The Project Settings command on the context menu of the project tree

Switching to the Options tab of the dialogue (which is titled Project Options) then provides access to all of the arguments available to the specified Tool Chain. Notice that both the assembler and linker options have been configured for us and, for this simple project, the default settings are sufficient.

See Also:

Project Settings

The Project Options dialogue showing the assembler options configured for the tutorial project

Since no further configuration is required we can go ahead and build our program. Select the build project option from the build menu or use the CTRL+F7 keyboard shortcut to start compilation. Output from the assembler will be written to the output window at the bottom of the IDE and you should see a 'Compiled Successfully' message when complete.

Assembler and linker output in the VSM Studio Output window, ending with a compiled successfully message

Simulation and Debugging

We now have both compiled firmware and a design to test it on. To start a simulation simply press the play button at the bottom left of the application.

The play button on the animation control panel

This will switch the application to the schematic module and the simulation will start running. Elapsed time and percentage CPU consumed are displayed on the status bar while the schematic will indicate the state of the circuit given the currently executing firmware.

You can also take live measurements while the simulation runs: the Multimeter and Scope buttons on the animation control panel launch the System Meter and the System Scope. See Testing and Debugging for more on the facilities available during a simulation.

In our example what you will see is that one of the traffic lights is green while the other is red. Unfortunately, they are not changing state due to a bug in our firmware. Time to look at the source code and do some debugging.

Setting Breakpoints

Start by clicking the Pause button on the animation control panel at the bottom of the application. This will pause the simulation and the focus will switch back to the VSM Studio IDE.

The pause button on the animation control panel

In the editing window area of the IDE you should see the executing program with a line overlay of the current instruction.

The paused source file with the current instruction highlighted by a line overlay

Looking at the program it can be seen that it loops back on itself on a repeating cycle. It would therefore be a good idea to set a breakpoint at the beginning of this loop at the call to the getmask routine.

The source listing at the start of the main loop and the call to the getmask routine

You can do this by clicking on the line and on the breakpoint icon. Alternatively, double clicking on the source line will set the breakpoint automatically.

A breakpoint marker set against a source line, alongside the debug toolbar

Breakpoints set anywhere in the project are listed together in the Breakpoints window, which you can open from the Edit menu or with the SHIFT+F9 shortcut key.

Having set the breakpoint, use the play button to run the simulation. It will in short order pause again, this time with the executing source line on our breakpoint.

The simulation paused with the executing source line on the breakpoint

A list of debugging commands is given on the debug menu and replicated on the debug toolbar but the one we will be using most often is F11 to single step through the program.

The single stepping commands on the debug menu, with Step Into Source Line and its F11 shortcut

If you try this now you should see the execution of the call instruction, leaving us at the entry point of the getmask instruction. Before we step further it's worth opening some more debug windows so that we can verify correct operation. From the debug menu, select the registers window which will appear as a tab at the bottom of VSM Studio. If we execute a single step operation (F11) now we should see that the value of the state variable is applied to the W register.

The PIC CPU Registers window showing the working register, status bits and port values

Stepping forward again, we will see that we 'land' in the expected place for adding an offset of zero onto our lookup table. A further step exits us from the getmask routine with a valid mask representing Green and Red, exactly what we see on the schematic traffic lights.

If we continue to single step (F11) while watching the registers display we can see that we write the correct mask to the port (PORTB equals our mask) and then that we correctly increment the state (value for W is incremented). The next instruction is designed to wrap the state around to zero when it is incremented above 3 and we need to look more closely at the state variable to verify this. The easiest way to do this is to open the watch window and then monitor the address of the state variable. You can open the watch window from the Debug Menu and it will appear as a tab at the bottom of VSM Studio.

The Watch Window docked with the registers and variables tabs at the bottom of VSM Studio

Now right click on the watch window and select add item by address. Type the variable name (state) and the address as 0x10 (as we specified in our firmware) and click Add then Done.

The Add Memory Item dialogue with the name, address, data type and display format for the state variable

Now single step the code beyond the setting of the state variable to check whether it increments correctly. We can see straight away that this is not happening. On closer investigation the problem is caused by ANDing with 4 instead of with 3. The states we want are 0,1,2,3 and any of those values when ANDed with 4 gives 0. The solution is simply to change the problem instruction to AND the state with 3 instead of with 4.

To do this we must first stop our debugging session (click the stop button on the animation control panel).

The stop button on the animation control panel and the corrected source line masking the state with 0x03

After changing the problem line of code we need to re-compile (CTRL+F7) and then run our simulation again. We will soon hit our breakpoint and can repeat our single stepping exercise to validate our change. Alternatively, right click on the editing window in VSM Studio and select the Clear All Breakpoints command (CTRL+F9) to allow the simulation to run freely. It should be clear from the schematic alone that things are now working.

The Clear All Breakpoints command and its CTRL+F9 shortcut on the editing window context menu

Active Popups

It is an inconvenience to have to switch between the source code you are stepping through and the design you are testing. Fortunately, you can bring parts of your schematic into your debugging IDE by using Active Popups.

With the simulation stopped switch to the schematic capture module and then select the Active Popup icon. Now drag out boxes around areas of interest on the schematic, in our case both sets of traffic lights.

Active popup boxes dragged out around both sets of traffic lights on the schematic

Now, when you run the simulation the animation takes place inside VSM Studio and, more importantly, when you single step debug you will see the effects of your code execution without having to switch to the schematic.

VSM Studio paused at a breakpoint with both sets of traffic lights animating in active popup panes

There is a great deal more power to the VSM simulation and debugging than we have covered here. We strongly recommend you work fully through the main VSM tutorial which works with the Microchip F1 Evaluation board using C language programming and therefore presents far more realistic debugging scenarios.